package com.lgqz.topsports.activity;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.RadioGroup;

import com.lgqz.topsports.R;
import com.lgqz.topsports.fragment.EventFragment;
import com.lgqz.topsports.fragment.HeadLineFragment;
import com.lgqz.topsports.fragment.NewsFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup radioGroup;
    private List<Fragment> fragmentList;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private NavigationView navigationView;
    private SlidingPaneLayout slidingPaneLayout;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.tab_main);
        navigationView = (NavigationView) findViewById(R.id.nav_main);
        slidingPaneLayout = (SlidingPaneLayout) findViewById(R.id.sliding_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_main);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.private_center);
        actionBar.setDisplayHomeAsUpEnabled(true);
          actionBar.setHomeButtonEnabled(true);
    navigationView.setItemIconTintList(null);
    radioGroup.setOnCheckedChangeListener(this);
    fragmentList = new ArrayList<>();
    fragmentList.add(new NewsFragment());
    fragmentList.add(new HeadLineFragment());
    fragmentList.add(new EventFragment());
    manager = getSupportFragmentManager();
    transaction = manager.beginTransaction();
    transaction.add(R.id.container_main, fragmentList.get(2));
    transaction.add(R.id.container_main, fragmentList.get(1));
    transaction.add(R.id.container_main, fragmentList.get(0));
    showFragment(0);
    transaction.commit();
}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                slidingPaneLayout.openPane();
                break;
        }
        return true;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        transaction = manager.beginTransaction();
        switch (checkedId) {
            case R.id.news_main:
                showFragment(0);
                break;
            case R.id.headline_main:
                showFragment(1);
                break;
            case R.id.event_main:
                showFragment(2);
                break;
        }
        transaction.commit();
    }

    private void showFragment(int position) {
        for (int i = 0; i < fragmentList.size(); i++) {
            if (i == position) {
                transaction.show(fragmentList.get(i));
            } else {
                transaction.hide(fragmentList.get(i));
            }

        }
    }

}
